package com.guanzi.server.controller;

import com.guanzi.server.entity.Order;
import com.guanzi.server.entity.vo.OrderExcelVO;
import com.guanzi.server.query.OrderListQuery;
import com.guanzi.server.service.IOrderService;
import com.guanzi.server.util.Excel;
import com.guanzi.server.util.RespBean;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author guanzi
 * @version 1.0
 * @date 2021/6/15 15
 */
@RequestMapping("/excel")
@RestController
public class ExcelController {

    @Autowired
    private IOrderService orderService;

    /**
     * 导出订单的Excel表
     *   前提： 当前导出的记录全部为同一个商家
     * @param response
     */
    @PostMapping("/order")
    public RespBean getOrderExcel(HttpServletResponse response, OrderListQuery orderListQuery){
        // excel标题
        String[] title = {"菜品","数量"};
        // 调用service层的方法获取待导出的集合(页面当前展示的，不包含分页)
        List<Order> list = orderService.getOrderList(orderListQuery).getList();
        // 将集合做处理，得到最后要输出的集合
        List<OrderExcelVO> outputValues =  orderService.getOutputValues(list);
        String fileName = System.currentTimeMillis() + "_" + list.get(0).getEStoreName() + "_订单记录表.xls";
        // sheet名
        String sheetName = list.get(0).getEStoreName() + "_订单记录";
        //内容
        String[][] values = new String[outputValues.size()][];
        for (int i = 0; i < outputValues.size(); i++){
            values[i] = new String[title.length];
            values[i][0] = outputValues.get(i).getFoodName();
            values[i][1] = outputValues.get(i).getFoodNumber() + "";
        }
        //调用Excel工具类方法
        HSSFWorkbook wb = Excel.getHSSFWorkbook(sheetName,title,values,null);
        //响应到客户端
        try {
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return RespBean.success("导出成功！");
    }


    /**
     * 内部方法
     * @param response
     * @param fileName
     */
    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try{
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.addHeader("Pargam","no-cache");
            response.addHeader("Cache-Control","no-cache");
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
